package vip.liux.front.application.service.product;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import vip.liux.application.dots.PagedResultDto;
import vip.liux.contracts.models.product.Product;
import vip.liux.contracts.repositories.product.ProductRepository;

import static vip.liux.application.PageKit.toPage;

@Service
public class GoodsService {

    private final ProductRepository productRepository;

    public GoodsService(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    public PagedResultDto<GoodsDto> list(GoodsQueryDto input) {
        Page<Product> page = productRepository.findByCatNo(input.getCatNo(), toPage(input));
        return new PagedResultDto<>(
                page.stream().map(AutoMapperProfile.INSTANCE::toGoodsDto).toList(),
                page.getTotalElements()
        );
    }
}
